package  
{
	import flash.text.TextField;
	import org.flexunit.internals.TraceListener;
	import org.flexunit.runner.notification.Failure;
	import org.flexunit.runner.Result;
	
	/**
	 * ...
	 * @author Victor Rosales
	 */
	public class TextFieldListener extends TraceListener 
	{
		private var textField:TextField;
		
		public function TextFieldListener(textField:TextField) 
		{
			this.textField = textField;
			log("TextfieldListener");
		}
		
		override protected function printHeader(runTime:Number):void 
		{
			log( "Time: " + elapsedTimeAsString(runTime) );
		}
		
		override protected function printFooter(result:Result):void 
		{
			//Determine if the result was a success
			if (result.successful ) {
			log( "OK (" + result.runCount + " test " + (result.runCount == 1 ? "" : "s") + ")" );
			} else {
			log( "FAILURES!!! Tests run: " + result.runCount + ", " + result.failureCount + " Failures:" );
			}
		}
		
		override protected function printFailures(result:Result):void 
		{
			var failures:Array = result.failures;
			//Determine if there are any failures to print
			if (failures.length == 0)
			return;
			if (failures.length == 1)
			log( "There was " + failures.length + " failure:" );
			else
			log("There were " + failures.length + " failures:" );

			//Print each failure
			for ( var i:int=0; i<failures.length; i++ ) {
			printFailure( failures[ i ], String( i+1 ) );
}
		}
		
		override protected function printFailure(failure:Failure, prefix:String):void 
		{
			log( prefix + " " + failure.testHeader + " " + failure.stackTrace );
		}
		
		protected function log(...args):void 
		{
			textField.appendText(args.join(",") + "\n");
		}
		
	}

}